package com.makeunion.gdlbsdemo.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.maps.AMapException;
import com.amap.api.maps.MapsInitializer;
import com.amap.api.maps.offlinemap.OfflineMapCity;
import com.amap.api.maps.offlinemap.OfflineMapManager;
import com.amap.api.maps.offlinemap.OfflineMapProvince;
import com.amap.api.maps.offlinemap.OfflineMapStatus;
import com.makeunion.gdlbsdemo.R;

import java.util.ArrayList;

/**
 * <p>write the description
 *
 * @author renjialiang
 * @version [版本号]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class OfflineMapActivity extends AppCompatActivity implements OfflineMapManager.OfflineMapDownloadListener{

    private OfflineMapManager offlineManager;

    private ArrayList<OfflineMapProvince> offlineMapProvinceList;

    private EditText mEtProvince;

    private EditText mEtCity;

    private TextView mTxCityCode;

    private String cityCode;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_offline_map);

        mEtProvince = (EditText)findViewById(R.id.et_province);
        mEtCity = (EditText)findViewById(R.id.et_city);
        mTxCityCode = (TextView)findViewById(R.id.tx_city_code);

        offlineManager = new OfflineMapManager(this, this);
        offlineMapProvinceList = offlineManager.getOfflineMapProvinceList();
    }

    @Override
    public void onDownload(int status, int completeCode, String downName) {
        switch (status) {
            case OfflineMapStatus.SUCCESS:

                break;
            case OfflineMapStatus.LOADING:

                break;
            case OfflineMapStatus.UNZIP:

                break;
            case OfflineMapStatus.WAITING:

                break;
            case OfflineMapStatus.PAUSE:

                break;
            case OfflineMapStatus.STOP:

                break;
            case OfflineMapStatus.ERROR:

                break;
            case OfflineMapStatus.EXCEPTION_AMAP:

                break;
            case OfflineMapStatus.EXCEPTION_NETWORK_LOADING:

                break;
            case OfflineMapStatus.EXCEPTION_SDCARD:

                break;
            default:
                break;
        }
    }

    @Override
    public void onCheckUpdate(boolean hasNew, String name) {
        Toast.makeText(this, "hasNew=" + hasNew + ", name=" + name, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onRemove(boolean success, String name, String describe) {
        Toast.makeText(this, "success=" + success + ", name=" + name + ", describe=" + describe, Toast.LENGTH_SHORT).show();
    }

    /**
     * 校验城市
     */
    public void getCityCodeByCityName(View view) {
        String provinceName = mEtProvince.getText().toString().trim();
        String cityName = mEtCity.getText().toString().trim();

        if(TextUtils.isEmpty(provinceName)) {
            toast("省份名不能为空");
            return;
        }
        if(TextUtils.isEmpty(cityName)) {
            toast("城市名不能为空");
            return;
        }

        OfflineMapProvince findProvince = null;
        for (OfflineMapProvince province : offlineMapProvinceList) {
            if(provinceName.equals(province.getProvinceName())) {
                findProvince = province;
                break;
            }
        }
        if(findProvince == null) {
            toast("省份不存在");
            return;
        }

        OfflineMapCity findCity = null;
        ArrayList<OfflineMapCity> cityList = findProvince.getCityList();
        for (OfflineMapCity city : cityList) {
            if(cityName.equals(city.getCity())) {
                findCity = city;
                break;
            }
        }
        if(findCity == null) {
            toast("城市不存在");
            return;
        }

        cityCode = findCity.getCode();
        mTxCityCode.setText(cityCode);
    }

    /**
     * 开始下载
     */
    public void startDownload(View view) {
        try {
            offlineManager.downloadByCityCode(cityCode);
//            offlineManager.downloadByCityName("");
//            offlineManager.downloadByProvinceName("");
        } catch (AMapException e) {
            e.printStackTrace();
        }
    }

    /**
     * 停止当前下载
     */
    public void pauseCurrent(View view) {
        offlineManager.pause();
    }

    /**
     * 停止全部下载
     */
    public void stopAll(View view) {
        offlineManager.stop();
    }

    /**
     * 更改存储路径
     */
    public void changeStorage(View view) {
        MapsInitializer.sdcardDir = "/sdcard/xx/xx/";
    }

    /**
     * 获取全国城市列表
     */
    public void getCityList(View view) {
        ArrayList<OfflineMapCity> cities = offlineManager.getOfflineMapCityList();
    }

    /**
     * 获取全国省份列表
     */
    public void getProvinceList(View view) {
        ArrayList<OfflineMapProvince> provinces = offlineManager.getOfflineMapProvinceList();
    }

    /**
     * 获取已下载的城市列表
     */
    public void getDownloadedCityList(View view) {
        ArrayList<OfflineMapCity> cities = offlineManager.getDownloadOfflineMapCityList();
    }

    /**
     * 获取已下载的省份列表
     */
    public void getDownloadedProvinceList(View view) {
        ArrayList<OfflineMapProvince> provinces = offlineManager.getDownloadOfflineMapProvinceList();
    }

    /**
     * 获取下载中和待下载的城市列表
     */
    public void getDownloadingCityList(View view) {
        ArrayList<OfflineMapCity> cities = offlineManager.getDownloadingCityList();
    }

    /**
     * 获取下载中和待下载的省份列表
     */
    public void getDownloadingProvinceList(View view) {
        ArrayList<OfflineMapProvince> provinces = offlineManager.getDownloadingProvinceList();
    }

    /**
     * 更新离线地图
     */
    public void updateOfflineMap(View view) {
        try {
            offlineManager.updateOfflineCityByCode(cityCode);
            offlineManager.updateOfflineCityByName("");
            offlineManager.updateOfflineMapProvinceByName("");
        } catch (AMapException e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除离线地图
     */
    public void deleteOfflineMap(View view) {
        offlineManager.remove("");
    }

    public void log(String log) {
        Log.e("TAG", log);
    }

    public void toast(String toast) {
        Toast.makeText(this, toast, Toast.LENGTH_SHORT).show();
    }
}
